package com.wbpay.sdk.pay.wxplugin;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.wbpay.sdk.constant.Constants;
import com.wbpay.sdk.pay.IPay;
import com.wbpay.sdk.utils.Utils;

import java.io.File;

/**
 * Created by Administrator on 2018/2/26.
 */

public class WxPluginHelper {

    private Context mContext;
    private IPay mPay;

    public WxPluginHelper(Context context) {
        mContext = context;
    }

    public void setPay(IPay iPay) {
        mPay = iPay;
    }

    private void installWxPayPlugin() {
        Toast.makeText(mContext, Utils.getStringId(mContext, "jy_miss_wxpay_plugin_tip"),
                Toast.LENGTH_SHORT).show();
        File apkDir = null;
        if (mContext.getExternalCacheDir() != null) {
            apkDir = new File(mContext.getExternalCacheDir(), "apk");
        }
        if (apkDir == null) {
            apkDir = new File(mContext.getCacheDir(), "apk");
        }
        if (apkDir != null && !apkDir.exists()) {
            apkDir.mkdirs();
        }
        File file = new File(apkDir, "wxpay-plugin.apk");
        Utils.copyAssets(mContext, "wxpay-plugin.apk", file.getAbsolutePath());
        if (file.exists()) {
            // 设置读权限
            file.setReadable(true, false);
            file.setExecutable(true, false);
        }
        Utils.showInstallActivity(mContext, file.getAbsolutePath());
    }

    private void uninstallWxPayPlugin() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
        filter.addDataScheme("package");
        mContext.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mContext.unregisterReceiver(this);
                if (intent == null) {
                    return;
                }
                String packageName = null;
                try {
                    packageName = intent.getData().getSchemeSpecificPart();
                } catch (Exception e) {
                }
                if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
                        && Constants.WXPAY_PLUGIN_PKGNAME.equals(packageName)) {
                    installWxPayPlugin();
                }
            }
        }, filter);

        Intent uninstall_intent = new Intent();
        uninstall_intent.setAction(Intent.ACTION_DELETE);
        uninstall_intent.setData(Uri.parse("package:" + Constants.WXPAY_PLUGIN_PKGNAME));
        mContext.startActivity(uninstall_intent);
    }

    /**
     * 检测微信支付插件是否安装
     *
     * @return
     */
    public boolean checkWechatPay() {
        if (mPay != null && mPay.getPayType() == IPay.PAY_TYPE_WECHATPAY) {
            String signStr = Utils.getSign(mContext, Constants.WXPAY_PLUGIN_PKGNAME);
            if (TextUtils.isEmpty(signStr)) {
                installWxPayPlugin();
                return false;
            } else if (!TextUtils.isEmpty(Constants.PLUGIN_MD5) && !Constants.PLUGIN_MD5.equalsIgnoreCase(signStr)) {
                // 支付插件被篡改时，让用户先下载旧的插件，再安装最新插件
                Toast.makeText(mContext, Utils.getStringId(mContext, "jy_wxpay_plugin_crack"),
                        Toast.LENGTH_LONG).show();
                uninstallWxPayPlugin();
                return false;
            }
        }
        return true;
    }

    /**
     * 检测微信是否已经安装
     *
     * @return
     */
    public boolean checkSwiftPay() {
        if (mPay != null && mPay.getPayType() == IPay.PAY_TYPE_SWIFT) {
            if (!Utils.isInstalled(mContext, Constants.WX_PKGNAME)) {
                Toast.makeText(mContext, Utils.getStringId(mContext, "jy_wechat_not_install"),
                        Toast.LENGTH_LONG).show();
                return false;
            }
        }
        return true;
    }
}
